package com.ksyun.campus.metaserver.controller;

import cn.hutool.core.date.DateUtil;
import com.ksyun.campus.common.constant.ZkConstant;
import com.ksyun.campus.common.domain.*;
import com.ksyun.campus.common.util.ByteConvertUtil;
import com.ksyun.campus.metaserver.services.MetaService;
import com.ksyun.campus.metaserver.services.RegistService;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController("/")
public class MetaController {

    @Autowired
    private CuratorFramework curatorFramework;

    @Autowired
    private RegistService registService;

    @Autowired
    private MetaService metaService;


    @RequestMapping("datalist")
    public ResponseEntity datalist(){
        List<DataServer> dataList = metaService.getDataList();
        return ResponseEntity.ok(dataList);
    }

    @RequestMapping("pick")
    public ResponseEntity pick() {
        DataServer dataServer = metaService.pickDataServer();
        return ResponseEntity.ok(dataServer);
    }

    /**
     * 查看文件元信息
     *
     * @param fileSystem
     * @param path
     * @return
     */
    @RequestMapping("stats")
    public ResponseEntity stats(@RequestHeader String fileSystem, @RequestParam String path) {
        return metaService.stats(fileSystem, path);
    }

    /**
     * 创建文件
     *
     * @param fileSystem
     * @param path
     * @return
     */
    @RequestMapping("create")
    public ResponseEntity createFile(@RequestHeader String fileSystem, @RequestParam String path) {
        return metaService.createFile(fileSystem, path);
    }

    /**
     * 创建目录
     *
     * @param fileSystem c d e
     * @param type       目录类型， 1磁盘 3普通目录
     * @param path
     * @return
     */
    @RequestMapping("mkdir")
    public ResponseEntity mkdir(@RequestHeader String fileSystem, @RequestParam int type, @RequestParam String path) {
        return metaService.mkdir(fileSystem, type, path);
    }

    /**
     * 查看目录
     *
     * @param fileSystem
     * @param path
     * @return
     */
    @RequestMapping("listdir")
    public ResponseEntity listdir(@RequestHeader String fileSystem, @RequestParam String path) {
        return metaService.listdir(fileSystem, path);
    }

    /**
     * 删除文件
     *
     * @param fileSystem
     * @param path
     * @return
     */
    @RequestMapping("delete")
    public ResponseEntity delete(@RequestHeader String fileSystem, @RequestParam String path) {
        return metaService.delete(fileSystem, path);
    }

    /**
     * 保存文件写入成功后的元数据信息，包括文件path、size、三副本信息等
     *
     * @param fileSystem
     * @return
     */
    @PostMapping("write")
    public ResponseEntity commitWrite(@RequestHeader String fileSystem, @RequestBody MetaWrite metaWrite) {
        return metaService.commitWrite(fileSystem, metaWrite);
    }

    /**
     * 根据文件path查询三副本的位置，返回客户端具体ds、文件分块信息
     *
     * @param fileSystem
     * @param path
     * @return
     */
    @RequestMapping("open")
    public ResponseEntity open(@RequestHeader String fileSystem, @RequestParam String path) {
        return metaService.open(fileSystem, path);
    }

    /**
     * 关闭退出进程
     */
    @RequestMapping("shutdown")
    public void shutdownServer() {
        //注销服务
        registService.unregister();
        System.exit(-1);
    }

}
